package com.iknow.ui.model;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.User;
import com.iknow.data.IKnowMessage;
import com.iknow.data.MessageSesstion;
import com.iknow.db.IKnowDataBase;
import com.iknow.image.ProfileImageCacheCallback;
import com.iknow.image.ProfileImageCacheManager;
import com.iknow.util.StringUtil;
import com.iknow.xmpp.service.Message;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import org.jivesoftware.smack.util.StringUtils;

public class SessionAdapter extends BaseAdapter
{
  public static SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
  private Context ctx = null;
  List<MessageSesstion> list;
  private LayoutInflater mInflater = null;

  public SessionAdapter(LayoutInflater paramLayoutInflater, List<MessageSesstion> paramList, Context paramContext)
  {
    this.list = null;
    this.mInflater = paramLayoutInflater;
    this.list = paramList;
    this.ctx = paramContext;
  }

  private void fillDataToView(MessageSesstion paramMessageSesstion, View paramView)
  {
    ViewHoler localViewHoler = (ViewHoler)paramView.getTag();
    localViewHoler.mUserName.setText(paramMessageSesstion.getRosterName());
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    if (StringUtil.compareTime(paramMessageSesstion.getTimestamp()))
    {
      localViewHoler.mDate.setText(sdf2.format(paramMessageSesstion.getTimestamp()));
      localViewHoler.mMsg.setText(paramMessageSesstion.getSubject());
      if (StringUtil.isEmpty(paramMessageSesstion.getAvatar()))
        break label158;
      localViewHoler.mUserHead.setImageBitmap(IKnow.mProfileImageCacheManager.getProfileImage(IKnowApiV4.formatFileUrl(paramMessageSesstion.getAvatar()), new CallBack(this, null)));
      int i = paramMessageSesstion.getUnread();
      if (i <= 0)
        break label180;
      localViewHoler.mUnReadCount.setVisibility(0);
      localViewHoler.mUnReadCount.setText(String.valueOf(i));
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          return;
          localViewHoler.mDate.setText(localSimpleDateFormat.format(paramMessageSesstion.getTimestamp()));
        }
        label158: localViewHoler.mUserHead.setImageDrawable(this.ctx.getResources().getDrawable(2130837551));
      }
      label180: localViewHoler.mUnReadCount.setVisibility(8);
    }
  }

  private View newView()
  {
    ViewHoler localViewHoler = new ViewHoler(this, null);
    View localView = this.mInflater.inflate(2130903090, null);
    localViewHoler.mUserHead = ((ImageView)localView.findViewById(2131296338));
    localViewHoler.mUserName = ((TextView)localView.findViewById(2131296287));
    localViewHoler.mDate = ((TextView)localView.findViewById(2131296288));
    localViewHoler.mMsg = ((TextView)localView.findViewById(2131296433));
    localViewHoler.mUnReadCount = ((TextView)localView.findViewById(2131296435));
    localView.setTag(localViewHoler);
    return localView;
  }

  public void addRecvMsg(Message paramMessage)
  {
    String str1;
    String str2;
    Iterator localIterator = this.list.iterator();
    if (!(localIterator.hasNext()))
    {
      str1 = StringUtils.parseBareAddress(IKnow.mUser.getUID());
      if (!(paramMessage.getFrom().equalsIgnoreCase(IKnow.mUser.getUID())))
        break label187;
      str2 = StringUtils.parseName(paramMessage.getTo());
    }
    while (true)
    {
      MessageSesstion localMessageSesstion2 = new MessageSesstion(paramMessage.getiKnowType(), str2, str1);
      localMessageSesstion2.setSubject(paramMessage.getBody());
      localMessageSesstion2.setTimestamp(paramMessage.getTimestamp());
      this.list.add(localMessageSesstion2);
      while (true)
      {
        MessageSesstion localMessageSesstion1;
        do
        {
          return;
          localMessageSesstion1 = (MessageSesstion)localIterator.next();
        }
        while ((!(paramMessage.getFrom().equalsIgnoreCase(localMessageSesstion1.getRosterID()))) && (!(paramMessage.getTo().equalsIgnoreCase(localMessageSesstion1.getUID()))));
        localMessageSesstion1.setSubject(paramMessage.getBody());
        localMessageSesstion1.setTimestamp(paramMessage.getTimestamp());
        IKnow.mIKnowDataBase.updateSession(paramMessage.getiKnowType(), paramMessage.getBody(), localMessageSesstion1.getUID(), localMessageSesstion1.getRosterID(), false, paramMessage.getTimestamp());
      }
      label187: str2 = StringUtils.parseName(paramMessage.getFrom());
    }
  }

  public void clear()
  {
    this.list.clear();
  }

  public void deleteMsg(int paramInt)
  {
    this.list.remove(paramInt);
  }

  public int getCount()
  {
    return this.list.size();
  }

  public MessageSesstion getItem(int paramInt)
  {
    return ((MessageSesstion)this.list.get(paramInt));
  }

  public long getItemId(int paramInt)
  {
    return paramInt;
  }

  public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
  {
    MessageSesstion localMessageSesstion = getItem(paramInt);
    if (paramView == null)
      paramView = newView();
    fillDataToView(localMessageSesstion, paramView);
    return paramView;
  }

  private class CallBack
  implements ProfileImageCacheCallback
  {
    public void refresh(, Bitmap paramBitmap)
    {
      this.this$0.notifyDataSetChanged();
    }
  }

  public class ConverViewClickListener
  implements View.OnClickListener
  {
    IKnowMessage msgItem = null;

    public ConverViewClickListener(, IKnowMessage paramIKnowMessage)
    {
      this.msgItem = paramIKnowMessage;
    }

    public void onClick()
    {
    }
  }

  private class ViewHoler
  {
    public TextView mDate;
    public TextView mMsg;
    public TextView mUnReadCount;
    public ImageView mUserHead;
    public TextView mUserName;
  }
}